package edu.scau.divineplace.util;

import com.google.common.base.Preconditions;
import edu.scau.divineplace.entity.enums.ResponseCodeEnum;
import edu.scau.divineplace.entity.response.Result;

/**
 * @Author: x1aolone
 * @Date: 2020/8/19 21:33
 */
public class ResultUtil {

    public static <T> T checkCodeAndGet(Result<T> result) {
        Preconditions.checkArgument(result.getCode() == ResponseCodeEnum.SUCCESS.getType());
        return result.getData();
    }

    public static Result buildWithoutData(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static Result build(int code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static Result success() {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.SUCCESS.getType());
        result.setMsg(ResponseCodeEnum.SUCCESS.getDescription());
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.SUCCESS.getType());
        result.setMsg(ResponseCodeEnum.SUCCESS.getDescription());
        result.setData(data);
        return result;
    }

    public static Result success(String msg) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.SUCCESS.getType());
        result.setMsg(msg);
        return result;
    }

    public static Result success(String msg, Object data) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.SUCCESS.getType());
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static Result error() {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.ERROR.getType());
        result.setMsg(ResponseCodeEnum.ERROR.getDescription());
        return result;
    }

    public static Result error(Object data) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.ERROR.getType());
        result.setMsg(ResponseCodeEnum.ERROR.getDescription());
        result.setData(data);
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.ERROR.getType());
        result.setMsg(msg);
        return result;
    }

    public static Result error(String msg, Object data) {
        Result result = new Result();
        result.setCode(ResponseCodeEnum.ERROR.getType());
        result.setMsg(msg);
        result.setData(data);
        return result;
    }




}
